package com.zxzhu.joyread.Model;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.Target;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by zxzhu on 2017/7/28.
 */

public class WelcomeModel implements IWelcomeModel {
    public interface OnPlayListener{
        void onStart();
        void onFinished();
    }

    @Override
    public void setGif(Context context, @DrawableRes int resorce, ImageView imageView, final OnPlayListener listener) {
        Glide.with(context).load(resorce).diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<Integer, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        if (listener!=null) {
                            listener.onStart();
                        }
                        rx.Observable.just(resource)
                                .flatMap(new Func1<GlideDrawable, Observable<?>>() {
                                    @Override
                                    public Observable<?> call(GlideDrawable glideDrawable) {

                                        int duration = 0;
                                        try {
                                            GifDrawable gifDrawable = (GifDrawable) glideDrawable;
                                            GifDecoder decoder = gifDrawable.getDecoder();
                                            for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
                                                duration += decoder.getDelay(i);                            }
                                        } catch (Throwable e) {
                                        }
                                        return Observable.just(null).delay(duration, TimeUnit.MILLISECONDS);
                                    }
                                })
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribeOn(Schedulers.io())
                                .subscribe(new Action1<Object>() {
                                    @Override
                                    public void call(Object o) {
                                        if (listener!=null) {
                                            listener.onFinished();
                                        }
                                    }
                                });

                        return false;
                    }
                })
                //仅仅加载一次gif动画
                //此处的参数 1 及时指明播放次数
                .into(new GlideDrawableImageViewTarget(imageView, 1));
    }
}
